package net.trustie.model;

import java.util.Date;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;

import org.apache.commons.codec.digest.DigestUtils;

import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;


@ExtractBy("//*div[@class='c_y']/div[@class='c_yc']/div[@class='c_ycr']/div[@class='c_yeeib']"
		+ "/div[@id='wp']/div[@id='ct']/div[@id='postlist']")
public class phpchina_Model implements AfterExtractor,ValidateExtractor {
	private int postId;

	private String postUrl;

	private String pageMD5;

	private String history = "0";

	private String extractTime;
	
	private Date crawledTime = null;
	
	@ExtractBy("*[@id='thread_subject']/text()")
	private String postTitle;
	
	@ExtractBy("//*table[1]/tbody/tr[1]/td[@class='pls ptn pbn']/div[@class='hm ptn']/span[2]/text()")
	private int viewNum;
	
	@ExtractBy("//*table[1]/tr/td[@class='pls ptn pbn']/div[@class='hm ptn']/span[5]/text()")
	private int replyNum;
	
	@ExtractBy("//*tbody/tr/td[@class='t_f']/allText()")
	
	private String postContent;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr/td[@class='pls']/div[@class='pls favatar']/div[@class='pi']"
			+ "/div[@class='authi']/a/text()")
	private String postAuthor;

	@ExtractBy("//*div[1]/table[1]/tbody/tr/td[@class='pls']/div[@class='pls favatar']/div[@class='pi']"
			+ "/div[@class='authi']/a/@href")
	private String postAuthorUrl;

	@ExtractBy("//*div[1]/table[1]/tbody/tr[1]/td[@class='plc']/div[@class='pi']/div[@class='pti']"
			+ "/div[@class='authi']/em/text()")
	private String postTime;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='recommend_add']/i/span[@id='recommendv_add']/text()")
	private int voteUp;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='recommend_subtract']/i/span[@id='recommendv_subtract']/text()")
	private int voteDown;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc']/div[@id='p_btn']/a[@id='k_favorite']/i/span[@id='favoritenumber']/text()")
	private int favoritedNum;
	
	@ExtractBy("//*div[1]/table[1]/tbody/tr[2]/td[@class='plc plm']/div[@id='p_btn']/a[@id='k_share']/i/span[@id='sharenumber']/text()")
	private int sharedNum;


	public void afterProcess(Page page) {
		// 处理postId
		this.postId = Integer.parseInt(StringHandler.matchRightString(
				page.getPageUrl(), "\\d+"));

		// 处理postUrl
		this.postUrl = page.getPageUrl();

		// 处理extractTime;
		this.extractTime = DateHandler.getExtractTime();
		
		this.crawledTime = page.getTime();
		//处理post_time
		String s1=postTime.replaceAll("发表于 ", "");
		this.postTime = DateHandler.formatAllTypeDate(s1, crawledTime);

		// 处理pageMD5
		this.pageMD5 = DigestUtils.md5Hex(this.postTitle);
	}

	public void validate(Page page) {
		if (StringHandler.isAtLeastOneBlank(this.postTitle, this.postAuthorUrl)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!DateHandler.canFormatToDate(this.postTime, this.extractTime)){
			page.setResultSkip(this, true);	
			return;
		}
	}

	public String getPostUrl() {
		return postUrl;
	}

	public void setPostUrl(String postUrl) {
		this.postUrl = postUrl;
	}

	public String getPageMD5() {
		return pageMD5;
	}

	public void setPageMD5(String pageMD5) {
		this.pageMD5 = pageMD5;
	}
	
	public Date getCrawledTime(){
		return crawledTime;
	}
	
	public void setCrawledTime(Date crawledTime){
		this.crawledTime = crawledTime;
	}

	public String getHistory() {
		return history;
	}

	public void setHistory(String history) {
		this.history = history;
	}

	public String getPostAuthor() {
		return postAuthor;
	}

	public void setPostAuthor(String postAuthor) {
		this.postAuthor = postAuthor;
	}

	public String getPostAuthorUrl() {
		return postAuthorUrl;
	}

	public void setNewsAuthorUrl(String postAuthorUrl) {
		this.postAuthorUrl = postAuthorUrl;
	}

	public String getPostTitle() {
		return postTitle;
	}

	public void setNewsTitle(String postTitle) {
		this.postTitle = postTitle;
	}

	public String getPostContent() {
		return postContent;
	}

	public void setPostContent(String postContent) {
		this.postContent = postContent;
	}


	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public int getPostId() {
		return postId;
	}

	public void setPostId(int newsId) {
		this.postId = newsId;
	}
	
	public int getViewNum(){
		return viewNum;
	}
	
	public void setViewNum(int viewNum){
		this.viewNum = viewNum;
	}
	
	public int getReplyNum(){
		return replyNum;
	}
	
	public void setReplyNum(int replyNum){
		this.replyNum = replyNum;
	}
	
	public String getPostTime(){
		return this.postTime;
	}
	
	public void setPostTime(String postTime){
		this.postTime = postTime;
	}
	
	public int getVoteUp(){
		return voteUp;
	}
	
	public void setVoteUp(int voteUp){
		this.voteUp = voteUp;
	}
	
	public int getVoteDown(){
		return voteDown;
	}
	
	public void setVoteDown(int voteDown){
		this.voteDown = voteDown;
	}
	
	public int getFavoritedNum(){
		return favoritedNum;
	}
	
	public void setFavoritedNum(int favoritedNum){
		this.favoritedNum = favoritedNum;
	}
	
	public int getSharedNum(){
		return sharedNum;
	}
	
	public void setSharedNum(int sharedNum){
		this.sharedNum = sharedNum;
	}
}
